/**
  ******************************************************************************
  * @file    sim900a.c
  * @author  ashuai0110
  * @version V2.3
  * @date    2024-07-01
  * @brief   SIM900A模组AT指令集
  *
  ******************************************************************************
  * @attention
  *
  * 版权声明:内容为编者(ashuai0110)原创,使用请注明出处,当然,你也可以不这样做^_^
  * 出处链接:https://gitee.com/ashuai0110/mcu_reuse_development_module.git
  *
  * Change Logs:
  * Date          Author        Notes
  * 2024-03-03    ashuai0110    完成基本内容
  *
  ******************************************************************************
  */

/* 包含头文件-----------------------------------------------------------------*/
#include "sim900a.h"

/**
 * @addtogroup devices
 * @{
 */

/**
 * @defgroup sim900a sim900a
 * @{
 */

/* 私有宏定义-----------------------------------------------------------------*/

/* 私有类型定义---------------------------------------------------------------*/

/* 私有变量-------------------------------------------------------------------*/
/**
 * @defgroup sim900a_local_variables sim900a local variables
 * @{
 */

static cmd_at_t sim900aCmdAt; /*!< at指令管理实例 */

static sim900a_t sim900a; /*!< sim900a管理实例 */

/**
 * @}
 */

/* 全局变量-------------------------------------------------------------------*/

/* 私有函数原型---------------------------------------------------------------*/


/**
 * @defgroup sim900a_global_functions sim900a global functions
 * @{
 */

/**
  * @brief  sim900a初始化
  *
  * @param  readFn  : 读数据接口
  *
  * @param  writeFn : 写数据接口
  *
  * @retval None
  */
void sim900a_init(cmd_at_rw_fn_t readFn, cmd_at_rw_fn_t writeFn)
{
    cmd_at_init(&sim900aCmdAt, SIM900A_HEAD, SIM900A_TAIL, readFn, writeFn, 3000, 0);
}

/**
  * @brief  sim900a置位外部接收标志
  *
  * @note   在接收完成的地方调用
  *
  * @param  None
  *
  * @retval None
  */
void sim900a_set_rec_flag(void)
{
    cmd_at_set_rec_flag(&sim900aCmdAt);
}

/**
  * @brief  校验sim卡状态
  *
  * @param  None
  *
  * @retval 校验结果
  *    @arg RET_OK  : sim卡正常
  *    @arg RET_ERR : sim卡异常
  *    @arg RET_ING : 运行中
  */
uint8_t sim900a_check_sim(void)
{
    uint8_t ret = RET_ING;
    
    ret = cmd_at_process_poll(&sim900aCmdAt, CMD_AT_RESERVE, CMD_AT_WAY_REC_FLAG, SIM900A_R_CPIN_RET, SIM900A_R_CPIN);
    if(RET_ERR == ret || RET_TIMEOUT == ret)
    {
        ret = RET_ERR;
        ERROR_LOG("check sim state error!");
    }
    
    return ret;
}

/**
  * @brief  启动发送短信
  *
  * @param  phone   : 手机号
  *
  * @param  message : 短信内容
  *
  * @retval 启动结果
  *    @arg RET_OK  : 启动成功
  *    @arg RET_ERR : 启动失败(上一个短信未发送完成)
  */
uint8_t sim900a_send_mes(const char *phone, const char *message)
{
    if(0 == sim900a.state)
    {
        sim900a.state = 1;
        strcpy(sim900a.phone, phone);
        sim900a.phone[strlen(phone)] = 0;
        strcpy(sim900a.message, message);
        sim900a.message[strlen(message)] = 0;
        
        return RET_OK;
    }
    
    return RET_ERR;
}

/**
  * @brief  发送短信流程轮询处理
  *
  * @param  None
  *
  * @retval 发送结果
  *    @arg RET_OK  : 发送成功
  *    @arg RET_ERR : 发送失败
  *    @arg RET_ING : 运行中
  */
uint8_t sim900a_send_mes_poll(void)
{
    uint8_t ret = RET_ING;
    
    switch(sim900a.state)
    {
        case 1: /* 查询sim卡状态  Tx:AT+CPIN?\r\n  Rx:OK */
            ret = cmd_at_process_poll(&sim900aCmdAt, CMD_AT_RESERVE, CMD_AT_WAY_REC_FLAG, SIM900A_R_CPIN_RET, SIM900A_R_CPIN);
            if(RET_ERR == ret || RET_TIMEOUT == ret)
            {
                ret = RET_ERR;
                sim900a.state = 0;
                ERROR_LOG("check sim state error!");
            }
            else if(RET_OK == ret)
            {
                sim900a.state++;
            }
            break;
        case 2: /* 设置短信文本模式  Tx:AT+CMGF=1\r\n  Rx:OK */
            ret = cmd_at_process_poll(&sim900aCmdAt, CMD_AT_RESERVE, CMD_AT_WAY_REC_FLAG, SIM900A_W_CMGF_RET, SIM900A_W_CMGF, 1);
            if(RET_ERR == ret || RET_TIMEOUT == ret)
            {
                ret = RET_ERR;
                sim900a.state = 0;
                ERROR_LOG("set message txt mode error!");
            }
            else if(RET_OK == ret)
            {
                sim900a.state++;
            }
            break;
        case 3: /* 设置目标手机号  Tx:AT+CMGS="18812341234"\r\n  Rx:> */
            ret = cmd_at_process_poll(&sim900aCmdAt, CMD_AT_RESERVE, CMD_AT_WAY_REC_FLAG, SIM900A_W_CMGS_RET, SIM900A_W_CMGS, sim900a.phone);
            if(RET_ERR == ret || RET_TIMEOUT == ret)
            {
                ret = RET_ERR;
                sim900a.state = 0;
                ERROR_LOG("set phone num error!");
            }
            else if(RET_OK == ret)
            {
                sim900a.state++;
            }
            break;
        case 4: /* 设置短信内容  Tx:this message  Rx:> */
            ret = cmd_at_process_poll(&sim900aCmdAt, CMD_AT_RESERVE, CMD_AT_WAY_REC_FLAG, SIM900A_W_CMGS_RET, "%s\r\n", sim900a.message);
            if(RET_ERR == ret || RET_TIMEOUT == ret)
            {
                ret = RET_ERR;
                sim900a.state = 0;
                ERROR_LOG("set message data error!");
            }
            else if(RET_OK == ret)
            {
                sim900a.state++;
            }
            break;
        case 5: /* 确认发送 Tx:0x1A  Rx:OK */
            sim900a.phone[0] = SIM900A_W_SEND_MES;
            sim900a.phone[1] = 0;
            ret = cmd_at_process_poll(&sim900aCmdAt, CMD_AT_RESERVE, CMD_AT_WAY_REC_FLAG, NULL, "%s", sim900a.phone);
            if(RET_ERR == ret || RET_TIMEOUT == ret)
            {
                ret = RET_OK;
                sim900a.state = 0;
            }
            else if(RET_OK == ret)
            {
                sim900a.state = 0;
            }
            break;
    }
    
    return ret;
}

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */
